<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 用户筛选器
 * 
 * @package 	htdocs
 * @subpackage  quanliadmin
 * @author  	create by allen.qiang
 */

quanli_require('module/base/common/quanliCommon.class.php');

class pa_userSelector{
	public function execute() {
		global $G_PGLOB ;
        if (isset($_REQUEST['operFlg'])) {
        	
            switch($_REQUEST['operFlg']) {

                case 1: //用户筛选器列表
                    $this->selectorList();
                break;
                case 2: //用户筛选器添加
                    $this->selectorAdd();
                break;
                case 3: //用户筛选器编辑
                    $this->selectorEdit();
                break;
                case 4: //用户筛选器详情查看
					$this->selectorView();
                break;   
				default:
					$G_PGLOB->smarty->assign('error_msg', '参数错误，请审核后再操作');
					$G_PGLOB->smarty->display('comm/error.tpl');
            }
        }
    }
	
	 /**
     * 用户筛选器列表
     * @author kangronghua
     * 
     */
    private function selectorList() {
        global $G_PGLOB;
        
        $G_PGLOB->smarty->display('promotion/userSelector_list.tpl');
    }
    
    /**
     * 筛选器添加 
     *
     */
    private function selectorAdd() {
    	global $G_PGLOB;
    	
    	$this->getFilter();
    }
    
    /**
     * 筛选器编辑 
     *
     */
    private function selectorEdit() {
    	global $G_PGLOB;
    	
    	//获取参数
    	$selectorID = isset($_REQUEST['selectorID']) ? trim($_REQUEST['selectorID']) : null;
    	if(empty($selectorID)){
    		$G_PGLOB->smarty->display('comm/error.tpl');
    		die();
    	}	
    		
    	//获取筛选器信息
    	$selectorInfo = new entity('cust_selector');
    	$selectorInfo->selectID = $selectorID;
    	if(!$G_PGLOB->DBCA->fetch($selectorInfo)){
    		$G_PGLOB->smarty->display('comm/error.tpl');
    		die();
    	}	
    		
    	//获取筛选规则信息
    	$selectorRule = $G_PGLOB->DBCA->stmt_query(sprintf('SELECT * FROM cust_selector_detail WHERE selectID = %d', $selectorID));
    	if($selectorRule){
    		foreach ($selectorRule as $k=>$rule){
    			$rule->selectorText = '';
    			$rule->optionArray = '';
    			if($rule->selectType == 1){
    				$selectorArr = explode(',', $rule->selector);
    				for ($m=0; $m<count($selectorArr); $m++){
    					//查询用户等级
    					$cust_rank = new entity('cust_rank');
    					$cust_rank->rankID = $selectorArr[$m];
    					if($G_PGLOB->DBCA->fetch($cust_rank))
    						$rule->selectorText .= $cust_rank->rankName . ',';
    				}
    				$rule->selectorText = mb_substr($rule->selectorText, 0, -1, 'UTF-8');
    			}else if($rule->selectType == 2){
    				$custData = array();
    				$custArray = explode(',', $rule->selector);
    				for ($n=0; $n<count($custArray); $n++){
    					$cust_master = new entity('cust_master');
    					$cust_master->custID = $custArray[$n];
    					if($G_PGLOB->DBCA->fetch($cust_master))
    						$custData[$cust_master->pcustID] = $custArray[$n];
    					
    				}
    				$rule->optionArray = json_encode($custData);
    			}
    		}
    	}

    	$G_PGLOB->smarty->assign('selectorInfo', $selectorInfo);
    	$G_PGLOB->smarty->assign('selectorRule', $selectorRule);
    	$this->getFilter();
    } 
    
    /**
     * 获取用户筛选器所需信息
     *
     */
    public function getFilter(){
    	global $G_PGLOB;
    	
    	//获取所有会员信息
    	$rankData = $G_PGLOB->DBCA->stmt_query('SELECT rankID,rankName FROM cust_rank WHERE useFlg = 1');
    	
    	$G_PGLOB->smarty->assign_by_ref('rankData', $rankData);
    	$G_PGLOB->smarty->display('promotion/userSelector_edit.tpl');
    }
    
    /**
     * 商品筛选器详情查看
     *
     */
    public function selectorView(){
    	global $G_PGLOB;
    	
    	//获取参数
    	$selectID = isset($_REQUEST['selectID']) ? trim($_REQUEST['selectID']) : null;
    	if(empty($selectID)){
    		$G_PGLOB->smarty->display('comm/error.tpl');
    		die();
    	}
    	
    	$commHandler = new commHandler($G_PGLOB->DBCA);
		
		$arg_array = array('selectID' => $selectID);
		
		$result_detail = $commHandler->search($arg_array, 'cust_selector_detail');
		if($result_detail){
			$detail->rankName = '';
			$detail->ruleText = '';
			$detail->typeText = '';
			foreach ($result_detail as $detail){
				if($detail->selectType == 1){
	    			$rankArray = explode(',', $detail->selector);
	    			for ($i=0; $i < count($rankArray); $i++){
	    				$cust_rank = new entity('cust_rank');
	    				$cust_rank->rankID = $rankArray[$i];
	    				if($G_PGLOB->DBCA->fetch($cust_rank))
	    					$detail->rankName .= $cust_rank->rankName . ',';
	    			}
	    			
	    			$detail->rankName = mb_substr($detail->rankName, 0, -1, 'UTF-8');
	    		}else 
	    			$detail->rankName = $detail->selector;
	    		
	    		if($detail->ruleFLg == 0)
	    			$detail->ruleText = '排斥';
	    		else if($detail->ruleFLg == 1)
	    			$detail->ruleText = '包含';
	    			
	    		if($detail->selectType == 1)
	    			$detail->typeText = '用户等级';
	    		else if($detail->selectType == 2)
	    			$detail->typeText = '客户名称';
			}
		}

		$G_PGLOB->smarty->assign_by_ref('result_detail', $result_detail);
    	$G_PGLOB->smarty->display('promotion/userSelector_view.tpl');
    }
}
?>